package org.jsrml.core.supplier.service;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.jsrml.common.persistence.BaseDao;
import org.jsrml.core.base.command.SaveAuthAccountCo;
import org.jsrml.core.base.entity.AuthAccount;
import org.jsrml.core.base.service.AuthAccountService;
import org.jsrml.core.supplier.command.DoVendorStatusCo;
import org.jsrml.core.supplier.command.RemoveVendorCo;
import org.jsrml.core.supplier.command.SaveVendorCo;
import org.jsrml.core.supplier.entity.Vendor;
import org.jsrml.core.supplier.exception.SupplierException;
import org.jsrml.core.supplier.qo.VendorQO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class VendorService extends BaseDao<Vendor, VendorQO> {

	@Autowired
	private AuthAccountService authAccountService;
	
	public Vendor create(SaveVendorCo command) throws SupplierException {
		
		Vendor vendor = new Vendor();
		vendor.create(command);
		
		/**
		 * 创建账号
		 */
		if (StringUtils.isBlank(command.getLoginName())) {
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "工作人员登录账号需要填写！");
		}

		SaveAuthAccountCo command2 = new SaveAuthAccountCo();
		command2.setDomainId(vendor.getId());
		command2.setClientType("vendor");
		command2.setLoginName(command.getLoginName());
		command2.setPassword("123456");

		AuthAccount account = new AuthAccount();
		account.create(command2);
		authAccountService.save(account);
		
		vendor.getInfo().setAuthAccountId(account.getId());
		
		save(vendor);
		return vendor;
	}

	public Vendor modify(SaveVendorCo command) throws SupplierException {

		if (StringUtils.isBlank(command.getVendorId())) {
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "供应商id不能为null！");
		}

		Vendor vendor = get(command.getVendorId());

		if (vendor == null) {
			throw new SupplierException(SupplierException.VENDOR_NOT_FOUND, "供应商没有找到！");
		}

		vendor.modify(command);
		save(vendor);
		return vendor;
	}

	public void remove(RemoveVendorCo command) throws SupplierException {
		if (StringUtils.isBlank(command.getVendorId())) {
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "供应商id不能为null！");
		}

		Vendor vendor = get(command.getVendorId());

		if (vendor == null) {
			throw new SupplierException(SupplierException.VENDOR_NOT_FOUND, "供应商没有找到！");
		}

		vendor.remove();
		save(vendor);
	}

	@Transactional(rollbackFor = SupplierException.class)
	public void plRemove(RemoveVendorCo command) throws SupplierException {
		if (command.getVendorIds() == null)
			return;

		for (String id : command.getVendorIds()) {
			Vendor vendor = get(id);
			if (vendor == null) {
				throw new SupplierException(SupplierException.VENDOR_NOT_FOUND, "供应商没有找到！");
			}
			vendor.remove();
			save(vendor);
		}
	}

	/**
	 * 修改供应商状态
	 * @param command
	 * @throws SupplierException
	 */
	public void doVendorStatus(DoVendorStatusCo command) throws SupplierException{
		
		if (StringUtils.isBlank(command.getVendorId())) {
			throw new SupplierException(SupplierException.NECESSARY_PARAM_NULL, "供应商id不能为null！");
		}
		
		Vendor vendor = get(command.getVendorId());
		if (vendor == null) {
			throw new SupplierException(SupplierException.VENDOR_NOT_FOUND, "供应商没有找到！");
		}
		
		vendor.doVendorStatus(command);
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, VendorQO qo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Class<Vendor> getEntityClass() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
